package com.mgetool.ui.animation;

import java.util.Vector;

import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.GC;

import com.mgetool.ui.util.PointF;


/**
 * 动画套件 控制多个动画播放
 * @author wangbin
 *
 */
public class IAnimationSuit extends IAnimation implements IAnimationListener{
	protected Vector<IAnimation> animations;
	protected int curPlayIndex;
//	public IAnimationSuit(){
//		super();
//	}
	
	/**
	 * 添加一个动画
	 * @param animation
	 */
	public void addAnimation(IAnimation animation){
		if(animation != null){
			animation.setListener(this);
			animations.addElement(animation);
		}
	}
	/**
	 * 将指定类型的动画提至顶部
	 * @param type
	 */
	public void upAnimatonToTop(byte type){
		IAnimation animation = getAnimationByType(type);
		animations.remove(animation);
		animations.addElement(animation);
	}
	
	/**
	 * 将指定类型的动画降至底部
	 * @param type
	 */
	public void upAnimatonToBottom(byte type){
		IAnimation animation = getAnimationByType(type);
		animations.remove(animation);
		animations.insertElementAt(animation, 0);
	}
	
	@Override
	public void setPosition(int x, int y) {
		// TODO Auto-generated method stub
		for(int i = 0; i < animations.size(); i++){
			animations.elementAt(i).setPosition(x, y);
		}
		super.setPosition(x, y);
	}
	@Override
	public PointF getCurPoint() {
		// TODO Auto-generated method stub
		for(int i = 0; i < animations.size(); i++){
			IAnimation animation = animations.elementAt(i);
			if(animation.isPlay()){
				return animation.getCurPoint();
			}
		}
		if(animations.size() > 0){
			return animations.elementAt(animations.size() - 1).getCurPoint();
		}
		return null;
	}

	/**
	 * 播放下一个动画
	 */
	public void playNext(){
		if(curPlayIndex + 1 < animations.size()){
			curPlayIndex++;
			IAnimation animation = getAnimationByIndex(curPlayIndex);
			if(!animation.isPlay()){
				animation.play();
			}
		}else{
			if(loopNum == -1){
				curPlayIndex = -1;
				playNext();
			}else{
				setEnd(true);
			}
		}
	}
	
	/**
	 * 得到指定类型的动画
	 * @param type
	 * @return
	 */
	public IAnimation getAnimationByType(byte type){
		for(int i = 0; i < animations.size(); i++){
			IAnimation animation = animations.elementAt(i);
			if(animation.getType() == type){
				return animation;
			}
		}
		return null;
	}
	
	/**
	 * 得到指定索引上的动画
	 * @param idx
	 * @return
	 */
	public IAnimation getAnimationByIndex(int idx){
		if(idx != -1 && idx < animations.size()){
			return animations.elementAt(idx);
		}
		return null;
	}
	
	@Override
	public void appendFrame(Frame frame) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Frame getCurFrame() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isEnd() {
		// TODO Auto-generated method stub
		return super.isEnd();
	}

	@Override
	public boolean isPlay() {
		// TODO Auto-generated method stub
		return super.isPlay();
	}

	@Override
	public void nextFrame() {
		// TODO Auto-generated method stub
		
	}

	public void playFromIndex(int index){
		if(index >= 0 && index < animations.size()){
			curPlayIndex = index;
			IAnimation animation = animations.elementAt(index);
			animation.play();
			setPlay(true);
		}
	}
	
	public void playAll(){
		for(int i = 0 ; i < animations.size(); i++){
			animations.elementAt(i).play();
		}
		setPlay(true);
	}
	
	@Override
	public void play() {
		// TODO Auto-generated method stub
		playFromIndex(0);
	}

	@Override
	public void reset() {
		// TODO Auto-generated method stub
		curPlayIndex = 0;
	}

	@Override
	public void setPlay(boolean isPlay) {
		// TODO Auto-generated method stub
		super.setPlay(isPlay);
	}

	@Override
	public void stop() {
		// TODO Auto-generated method stub
		setPlay(false);
		for(int i = 0; i < animations.size(); i++){
			animations.elementAt(i).stop();
		}
	}

	@Override
	protected void updataAlpha() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updataFramIdx() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updataLoop() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void updataPosition() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void updataScaling() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		animations = new Vector<IAnimation>();
		curPlayIndex = 0;
	}

	@Override
	public void onDraw(GC gc) {
		// TODO Auto-generated method stub
		if(isPlay()){
			onDrawBackGround(gc);
			synchronized(animations){
				if(animations != null){
					for(int i = 0; i < animations.size(); i++){
						IAnimation animation = animations.elementAt(i);
						if(animation.isPlay()){
							animation.onDraw(gc);
						}
					}
				}
			}
//			if(getType() == 8){
//				Log.d(getClass().getSimpleName(), "curPosition.x " + getCurPoint().x + " curPosition.y " + getCurPoint().y);
//			}
		}
	}

	
	
	@Override
	public boolean isClicked(int x, int y) {
		// TODO Auto-generated method stub
		if(animations != null){
			for(int i = 0; i < animations.size(); i++){
				IAnimation animation = animations.elementAt(i);
				if(animation.isPlay()){
					boolean isUesed = animation.isClicked(x, y);
					if(isUesed){
						return true;
					}
				}
			}
		}
		return false;
	}
	@Override
	public boolean onKey(int state, KeyEvent keyEvent) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onMouseEvent(int state, MouseEvent event) {
		// TODO Auto-generated method stub
		for(int i = 0; i < animations.size(); i++){
			if(animations.elementAt(i).onMouseEvent(state, event)){
				noticeOnTouchListener();
				return true;
			}
		}
		return false;
	}

	@Override
	public void onUpdate() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void animationEnd(IAnimation animation) {
		// TODO Auto-generated method stub
		animation.stop();
		playNext();
	}

	@Override
	public void animationOnTouch(IAnimation animation) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void animationTimeOutListener(IAnimation animation) {
		// TODO Auto-generated method stub
		animation.stop();
		playNext();
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		synchronized(animations){
			for(int i = 0; i < animations.size(); i++){
				animations.elementAt(i).onDestroy();
			}
			animations.removeAllElements();
//			animations = null;
		}
	}
	
	/**
	 * 得到套件中所有的动画
	 * @return
	 */
	public Vector<IAnimation> getAnimations(){
		return animations;
	}

	@Override
	public void animationPlayIndex(IAnimation animation, int animationIdx, int frameIdx) {
		// TODO Auto-generated method stub
		noticeAnimationPlayIdx(curPlayIndex, frameIdx);
	}
	
	/**
	 * 清除所有动画
	 */
	public void removeAllAnimations(){
		if(animations != null){
			animations.removeAllElements();
		}
	}
	
}
